package login;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

import common.ClampFactory;



// 회원정보DB를 읽어 들여서 처리하기 위한 모든 제어구조를 가지고 있는 클래스. 
public class MemberMyBa {
	private SqlSession session; //커넥션 객체가 생성된 상태
	MemberVo memberVo;
	
	public MemberMyBa(){
		try {
			session = ClampFactory.getFactory().openSession(); //factory는 static형(왜?메모리에 이미 있음)이라 new를 만들지 않고도 바로 가져다 쓸수 있다.
			
		} catch (Exception ex) {
			//System.out.println("session error");
			ex.printStackTrace();
		}
		
	}
	
	public MemberVo login(){
		MemberVo vo = null;
		try {
			vo = (MemberVo) session.selectOne("member.login", memberVo);
			/*System.out.println(cnt);*/
			
			session.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return vo;
	}
	public MemberVo login2(){
		MemberVo vo = null;
		try {
			vo = (MemberVo) session.selectOne("member.login2", memberVo);
			session.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return vo;
	}
	
	
	/*public MemberVo view(){
		MemberVo vo = null;
		try {
			vo=(MemberVo)session.selectOne("member.view", memberVo.getSA_no()); //두번째 매개변수memberVo.getMid()의 파라미터값을 member.xml의 mapper에 
																		    	//member안의view에 전달,,
			session.close();	//세션을 바로닫아준다.	
		} catch (Exception ex) {
			ex.printStackTrace();
		}	
		return vo;
	}
	
	
	public ArrayList<MemberVo> list(){
		ArrayList<MemberVo> data = null;
		try {
			data = (ArrayList<MemberVo>)session.selectList("member.list", "%" + memberVo.getFind() + "%"); //selectList 를 썻다.. 값이 하나가 아니라서.
			session.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return data;
	}
	
	public int insert(){
		int cnt = 0;
		try {
			cnt = session.insert("member.insert", memberVo);
			if(cnt>0){
				session.commit();
			}else{
				session.rollback();
			}
			
			session.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return cnt;
	}
	
	
	public int delete(){
		int cnt=0;
		try {
			cnt = session.delete("member.delete", memberVo.getMid() );
			if(cnt>0){
				session.commit();
			}else{ 
				session.rollback();
			}
			
			session.close();	
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}			
		return cnt;
	}
	
	public int modify(){
		int cnt=0;
		System.out.println(memberVo.getMid() + memberVo.getNewPwd1() + memberVo.getPwd());  //값넘어오는지 찍어봄
		try {
			cnt = session.update("member.update", memberVo);
			if(cnt>0){
				session.commit();
			}else{ 
				session.rollback();
			}			
			session.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}			
		return cnt;
	}*/
	

	
	public MemberVo getMemberVo() {
		return memberVo;
	}

	public void setMemberVo(MemberVo memberVo) {
		this.memberVo = memberVo;
	}
		
	
	
}
